package com.xspace.android.xafcommon.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;

public abstract class SdcardDBHelper extends SDSQLiteOpenHelper {
	public SQLiteDatabase mDatabase = null;
	public static final String TB_DELETE = "DROP TABLE IF EXISTS ";

	/**
	 * @Description: TODO
	 * @param @param context
	 * @param @param dbPath
	 * @param @param name
	 * @param @param factory
	 * @param @param version
	 * @return
	 * @throws
	 */
	public SdcardDBHelper(Context context, String dbPath, String name,
			CursorFactory factory, int version) {
		super(context, dbPath, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	public void open() {
		try {
			if (mDatabase == null)
				mDatabase = getWritableDatabase();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void close() {
		if (null != mDatabase) {
			mDatabase.close();
			mDatabase = null;
		}
	}

	public synchronized int insertOrUpdate(String tableName,
			ContentValues values, String where) {
		open();
		int _ID = -1;
		try {
			Cursor cursor = mDatabase.query(tableName, null, where, null, null,
					null, null);
			if (cursor != null && cursor.moveToFirst()) {
				cursor.close();
				_ID = (int) mDatabase.update(tableName, values, where, null);
			} else {
				_ID = (int) mDatabase.insert(tableName, null, values);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
			close();
		}
		return _ID;
	}

	public synchronized int update(String tableName, ContentValues values,
			String where) {
		open();
		int _ID = -1;
		try {
			_ID = (int) mDatabase.update(tableName, values, where, null);
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
			close();
		}
		return _ID;
	}

	public synchronized Cursor query(String tableName, String where) {
		Cursor cur = null;
		try {
			open();
			cur = mDatabase.query(tableName, null, where, null, null, null,
					null);

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {

		}
		return cur;
	}

	public synchronized int delete(String tableName, String selection,
			String[] selectionArgs) {
		int result = -1;
		try {
			open();
			result = mDatabase.delete(tableName, selection, selectionArgs);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			close();
		}
		return result;
	}

	public synchronized int queryMaxId(String table) {
		int maxId = 0;
		try {
			open();
			Cursor cur;
			String strSql = "select max(_ID) AS _ID from " + table;
			cur = mDatabase.rawQuery(strSql, null);
			if (null != cur && cur.getCount() != 0) {
				cur.moveToFirst();
				maxId = cur.getInt(cur.getColumnIndex("_ID"));
			}
			if (null != cur) {
				cur.close();
				cur = null;
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			close();
		}
		return ++maxId;
	}

}
